package com.ssl.basics.list;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.CountDownLatch;

/**
 * 描述：
 *
 * @author shi_senlin
 * @create 2021/07/10 9:19
 */
public class SetSafe {
    public static void main(String[] args) throws InterruptedException {
        // Set<Integer> set = Collections.synchronizedSet(new HashSet<>());
        Set<Integer> set = new CopyOnWriteArraySet<>();
        CountDownLatch latch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int j = 0; j < 100; j++) {
                    set.add(j);
                }
                latch.countDown();
            }, "AA").start();
        }
        latch.await();
        System.out.println(set);
        System.out.println(set.size());
    }
}
